local skel = fk.CreateSkill {
  name = "lb__zaixian",
}

Fk:loadTranslationTable {
  ["lb__zaixian"] = "再现",
  [":lb__zaixian"] = "其他角色回合结束时，你可展示手牌并依次执行正序或倒序的前X项（X为手牌颜色与类型数之和且最多为4）：<br>" ..
      "1.重铸一张牌；<br>" ..
      "2.失去1点体力；<br>" ..
      "3.若本轮发动〖再现〗的次数不大于你的体力值，获得一个额外回合；<br>" ..
      "4.获得<a href=':lb__luandie'><font color='red'>〖乱蝶〗</font></a>，若已有则失去之。",
  ["#lb__zaixian"] = "再现：是否以正序执行〖再现〗",
  ["#lb__zaixian-cz"] = "再现：重铸一张牌",

  ["$lb__zaixian1"] = "随蝴蝶一起消散吧。",
  ["$lb__zaixian2"] = "早点给你个痛快。",
  ["$lb__zaixian3"] = "纠缠不清。",
  ["$lb__zaixian4"] = "这就让你解脱。",

  ["@lb__zaixian"] = "再现",
}

skel:addEffect(fk.TurnEnd, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target ~= player and player:hasSkill(skel.name) and
        player.phase == Player.NotActive and not player:isKongcheng()
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local list = {}
    local ld = player:hasSkill("lb__luandie")
    player:broadcastSkillInvoke(skel.name, math.random(1, 2) + (ld and 1 or 0))
    room:notifySkillInvoked(player, skel.name)
    player:showCards(player:getCardIds("h"))
    table.forEach(player:getCardIds("h"), function(id)
      local c = Fk:getCardById(id)
      table.insertIfNeed(list, c:getTypeString())
      table.insertIfNeed(list, c:getColorString())
    end)
    local n = math.min(#list, 4)
    if n == 4 and ld then return end
    local num = {}
    for i = 1, 4 do
      table.insert(num, i)
    end
    if not room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#lb__zaixian" }) then
      num = table.reverse(num)
    end
    if ld then
      num = table.slice(num, n + 1)
    else
      num = table.slice(num, 1, n + 1)
    end
    for _, v in ipairs(num) do
      if player.dead then break end
      if v == 1 then
        local cards = room:askToCards(player, {
          min_num = 1,
          max_num = 1,
          include_equip = true,
          skill_name = skel.name,
          prompt = "#lb__zaixian-cz",
          cancelable = false,
        })
        room:recastCard(cards, player, skel.name)
      elseif v == 2 then
        room:loseHp(player, 1, skel.name)
      elseif v == 3 and player:usedSkillTimes(skel.name, Player.HistoryRound) <= player.hp then
        player:gainAnExtraTurn()
      elseif v == 4 then
        local name = "lb__luandie"
        if ld then name = "-" .. name end
        room:handleAddLoseSkills(player, name)
      end
    end
  end,
})

return skel
